﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ZSZ.Core.Entities;

namespace ZSZ.EntityFramework.ModelConfig
{
    class HouseAppointmentConfig:EntityTypeConfiguration<HouseAppointmentEntity>
    {
        //public HouseAppointmentConfig()
        //{
        //    ToTable("T_HouseAppointments");
        //    HasOptional(h => h.User).WithMany().HasForeignKey(h=>h.UserId).WillCascadeOnDelete(false);
        //    HasRequired(h => h.House).WithMany().HasForeignKey(h=>h.HouseId).WillCascadeOnDelete(false);
        //    HasOptional(h => h.FollowAdminUser).WithMany().HasForeignKey(h=>h.FollowAdminUserId).WillCascadeOnDelete(false);
        //    Property(h => h.Name).IsRequired().HasMaxLength(20);
        //    Property(h => h.PhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);
        //    Property(h => h.Status).IsRequired().HasMaxLength(20);
        //    Property(h => h.RowVersion).IsRequired().IsRowVersion();
        //}

        public override void Map(EntityTypeBuilder<HouseAppointmentEntity> builder)
        {
            builder.ToTable("T_HouseAppointment");

            builder.Property(h => h.Name).IsRequired().HasMaxLength(20);
            builder.Property(h => h.PhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);
            builder.Property(h => h.Status).IsRequired().HasMaxLength(20);
            builder.Property(h => h.RowVersion).IsRequired().IsRowVersion();

            builder.HasOne(e => e.User).WithMany().HasForeignKey(h => h.UserId).OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.House).WithMany().HasForeignKey(h => h.HouseId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.HasOne(e => e.FollowAdminUser).WithMany().HasForeignKey(h => h.FollowAdminUserId)
                .OnDelete(DeleteBehavior.ClientSetNull);

        }
    }
}
